package sisi.payment;

import java.util.List;

import javax.persistence.EntityManager;


/**
 * PaymentsController.java - Controller class for the <i>payment term database table</i>. 
 * @author Pedro Gonzalez pedro@sisisoftware.it
 * @author Claudio Pelosi claudio@sisisoftware.it
 * @author SISI Software S.n.c. - www.sisisoftware.it
 * @version 1.0, 18/11/2013
 * 
 */

public class PaymentsController { 

	private EntityManager getEntityManager() {
		return new sisi.MyEmf().getEntityManager();
	}

	public Payment[] getPayment(String cerca) {
		List<Payment> payment = getPaymentList(cerca);
		return payment.toArray(new Payment[0]);
	}

	@SuppressWarnings("unchecked")
	public List<Payment> getPaymentList(String cerca) {

		String cWhere = "";
		if (cerca != null && !cerca.isEmpty()) {
			cWhere = " WHERE TRIM( BOTH FROM UPPER(c.terms) ) LIKE :cerca ";
		}

		EntityManager em = getEntityManager();
		try {
			javax.persistence.Query q = em
					.createQuery("select c from Payment as c " + cWhere
							+ " order by c.terms");
			
			if (cerca != null && !cerca.isEmpty()) {
				q.setParameter("cerca", '%'+cerca.trim().toUpperCase()+'%' );
			}
			
			List<Payment> ar1 = q.getResultList();
			return ar1;
		} finally {
			em.close();
		}
	}
	
	
	public Payment getPaymentxTerms(String terms) {

		String cWhere = " WHERE TRIM( BOTH FROM UPPER(c.terms) ) = :cerca ";

		EntityManager em = getEntityManager();
		try {
			javax.persistence.Query q = em
					.createQuery("select c from Payment as c " + cWhere
							+ " ");
			
				q.setParameter("cerca", terms.trim().toUpperCase() );
				q.setMaxResults(1); 
			
			Payment ar1 = new Payment();
			if (q.getResultList().size()==1){
				ar1 = (Payment)q.getSingleResult();	
			}
			return ar1;
		} finally {
			em.close();
		}
	}

	public Payment getPaymentxId(int id) {

		EntityManager em = getEntityManager();
		try {
			Payment paymentx = em.find(Payment.class, id);
			return paymentx;
		} finally {
			em.close();
		}
	}

	@SuppressWarnings("finally")
	public Payment addPayment(Payment payment) {
		EntityManager em = getEntityManager();
		try {
			em.getTransaction().begin();
			em.persist(payment);
			em.getTransaction().commit();
			payment = refreshPayment(payment);
		} finally {
			em.close();
			return payment;
		}
	}

	@SuppressWarnings("finally")
	public boolean updatePayment(Payment payment) {
		EntityManager em = getEntityManager();
		try {
			em.getTransaction().begin();
			Payment paymentx = em.find(Payment.class, payment.getId());
			paymentx.setDd(payment.getDd());
			paymentx.setId(payment.getId());
			paymentx.setTerms(payment.getTerms());			
			em.getTransaction().commit();
			em.flush();
		} finally {
			em.close();
			return false;
		}
	}

	@SuppressWarnings("finally")
	public Payment refreshPayment(Payment payment) {

		Payment paymentx = payment;
		EntityManager em = getEntityManager();
		try {
			em.getTransaction().begin();
			paymentx = em.find(Payment.class, payment.getId());
			em.flush();
			em.refresh(paymentx);
		} finally { 
			em.close();
			return paymentx;
		}
	}

	@SuppressWarnings("finally")
	public boolean removePayment(Payment payment) {
		EntityManager em = getEntityManager();
		try {
			em.getTransaction().begin();
			Payment paymentx = em.find(Payment.class, payment.getId());
			em.remove(paymentx);
			em.getTransaction().commit();
		} finally {
			em.close();
			return false;
		}
	}

}
